package com.letv.core.sysletvplayer.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.letv.android.sdk.R;
import com.letv.core.sysletvplayer.utils.Tools;

public class LetvSeekBar extends RelativeLayout implements android.widget.SeekBar.OnSeekBarChangeListener {

	private TextView mBeginTextView;
	private TextView mEndTextView;
	private android.widget.SeekBar mSeekBar;
	private FrameLayout mFrameLayout;
	private Drawable mThumb;
	private Context mContext;
	private String endTimes = "00:00";
	private int mEndTextViewLeftMargin = 0;

	private long mEndTime = 0;

    private OnSeekBarTouchListener mSeekBarTouchListener;

	public interface OnSeekBarChangeListener {
		void onProgressChanged(SeekBar VerticalSeekBar, int progress, boolean fromUser);

		void onStartTrackingTouch(SeekBar VerticalSeekBar);

		void onStopTrackingTouch(SeekBar VerticalSeekBar);
	}

    public interface OnSeekBarTouchListener{
        void onSeekBarTouch();
    }

    public void setOnSeekBarTouchListener(OnSeekBarTouchListener listener){
        mSeekBarTouchListener = listener;
    }

	private OnSeekBarChangeListener mOnSeekBarChangeListener;

	public LetvSeekBar(Context context) {
		this(context, null);
	}

	public LetvSeekBar(Context context, AttributeSet attrs) {
		this(context, attrs, android.R.attr.seekBarStyle);
	}

	public LetvSeekBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs, defStyle);
	}

	private void init(Context context, AttributeSet attrs, int defStyle) {
		mContext = context;
		
		mFrameLayout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.letv_local_player_seekbar, null);
		mSeekBar = (SeekBar) mFrameLayout.findViewById(R.id.play_seekbar_child);
		mBeginTextView = (TextView) mFrameLayout.findViewById(R.id.seek_start_time);
		mEndTextView = (TextView) mFrameLayout.findViewById(R.id.seek_end_time);

		RelativeLayout.LayoutParams paramsSeekBar = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		mFrameLayout.setLayoutParams(paramsSeekBar);

		mSeekBar.setOnSeekBarChangeListener(this);
		mThumb = getResources().getDrawable(R.drawable.letv_local_play_controller_seek_thumb);
		mSeekBar.setThumb(mThumb);
		mSeekBar.setThumbOffset(0);
        mSeekBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (mSeekBarTouchListener != null) {
                            mSeekBarTouchListener.onSeekBarTouch();
                        }
                        break;
                }
                return false;
            }
        });
		this.addView(mFrameLayout);
	}

	public int getMax() {
		return mSeekBar.getMax();
	}

	public int getProgress() {
		return mSeekBar.getProgress();
	}

	public void setEnable(boolean enabled) {
		mSeekBar.setEnabled(enabled);
	}

	public void setMax(int max) {
		mSeekBar.setMax(max);
	}

	public void setProgress(int progress) {
		mSeekBar.setProgress(progress);
		mBeginTextView.setText(Tools.stringForTime(progress * 1000));
	}

	public void setSecondaryProgress(int secondaryProgress) {
		mSeekBar.setSecondaryProgress(secondaryProgress);
	}

	public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
		mOnSeekBarChangeListener = l;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		View parent = (View) LetvSeekBar.this.getParent();
		if (LetvSeekBar.this.getVisibility() == VISIBLE && (parent != null && parent.getVisibility() == VISIBLE)) {
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mBeginTextView.getLayoutParams();
			mEndTextViewLeftMargin = getMeasuredWidth() - mEndTextView.getMeasuredWidth();
			if (mEndTextViewLeftMargin > 0 && mThumb.getBounds().right >= mEndTextViewLeftMargin) {
				mBeginTextView.setVisibility(View.INVISIBLE);
			} else {
				mBeginTextView.setVisibility(View.VISIBLE);
				int offset = mThumb.getBounds().left - mBeginTextView.getMeasuredWidth();
				params.leftMargin = offset < 0 ? 0 : offset;
			}
			if (mBeginTextView.getVisibility() == View.INVISIBLE) {
				mEndTextView.setText(Tools.stringForTime(progress * 1000) + "/" + endTimes);
			} else {
				mBeginTextView.setText(Tools.stringForTime(progress * 1000));
				mEndTextView.setText(endTimes);
				mBeginTextView.setLayoutParams(params);
			}
		}
		if (mOnSeekBarChangeListener != null) {
			mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		if (mOnSeekBarChangeListener != null) {
			mOnSeekBarChangeListener.onStartTrackingTouch(mSeekBar);
		}
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		if (mOnSeekBarChangeListener != null) {
			mOnSeekBarChangeListener.onStopTrackingTouch(mSeekBar);
		}
	}

	public void setBeginTime(long timeMs) {
		mBeginTextView.setVisibility(View.VISIBLE);
		mBeginTextView.setText(Tools.stringForTime(timeMs));
	}

	public void setEndTime(long timeMs) {
		endTimes = Tools.stringForTime(timeMs);
		mEndTextView.setVisibility(View.VISIBLE);
		mEndTextView.setText(endTimes);
		mEndTextViewLeftMargin = getMeasuredWidth() - mEndTextView.getMeasuredWidth();
	}
	@Override
	protected void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if(!Tools.isLandscape((Activity) mContext)){
//			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
//					UIs.zoomHeight(42));
//			mFrameLayout.setLayoutParams(params);
			mSeekBar.setOnSeekBarChangeListener(this);
			mThumb = getResources().getDrawable(R.drawable.letv_local_play_controller_seek_thumb);
			mSeekBar.setThumb(mThumb);
			mSeekBar.setThumbOffset(0);
		}
	}
}
